<template>
  <el-dialog title="修改密码" :visible.sync="visible" width="400px" @close="resetForm">
    <el-form :model="form" ref="formRef" label-width="100px">
      <el-form-item label="历史密码" prop="oldPassword" :rules="[{ required: true, message: '请输入历史密码', trigger: 'blur' }]">
        <el-input v-model="form.oldPassword" type="password" autocomplete="off" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword" :rules="[{ required: true, message: '请输入新密码', trigger: 'blur' }]">
        <el-input v-model="form.newPassword" type="password" autocomplete="off" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleChangePassword">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: { visible: Boolean },
  data() {
    return {
      form: { oldPassword: '', newPassword: '' }
    }
  },
  methods: {
    resetForm() {
      this.form.oldPassword = '';
      this.form.newPassword = '';
    },
    handleChangePassword() {
      this.$axios.post('/users/changePassword', this.form).then(resp => {
        if (resp && resp.code === 200) {
          this.$message.success('密码修改成功');
          this.visible = false;
        } else {
          this.$message.error(resp.msg || '密码修改失败');
        }
      });
    }
  }
}
</script>